package com.globaldom.web.sales;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.globaldom.model.Sales;
import com.globaldom.utils.Constants;
import com.globaldom.utils.PMF;
import com.globaldom.utils.SessionManager;

@Controller
@RequestMapping(value = "/getallparticularsales")
public class SalesGetAllUniqueController {
	public SalesGetAllUniqueController() {
	}

	@RequestMapping(value = "/getall", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> getAllSales(HttpSession session, @RequestParam int saleType, HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		
		String personID = SessionManager.getID(request, session);
		if(null==personID) 
			{
			map = new HashMap<String, String>();
			map.put(Constants.STATUS, "-ER");
			map.put(Constants.CODE, "401");
			return map;
			}
	PersistenceManager pmf = PMF.get().getPersistenceManager();
		try {
			Query query = pmf.newQuery(Sales.class, "saleType==saletype && personID!=pid");
			query.declareParameters("int  saletype");
			query.declareParameters("String pid");
			List<Sales> resources = ((List<Sales>) query.execute(saleType, personID));
		
			for (Sales sale : resources) {

				map.put(sale.getId().toString(),
						sale.getSaleType() + "/" + sale.getSaleAmount() + "/" + sale.getSalePrice());

			}
		} finally {
			pmf.close();
		}
		return map;

	}

}
